import md5 from "md5";
//https://xmgl.glodon.com
var host = "";

export default {
    getQueryParam(sign, params) {
        let querys = [];
        params.sign = sign;
        Object.keys(params).map(key => {
            querys.push(`${key}=${params[key]}`);
        });
        return querys.join("&");
    },
    getSign(body) {
        function getSignString(params) {
            let arr = [];
            for (let key in params) {
                if (!key || !params[key]) {
                    continue;
                }
                arr.push(key + params[key]);
            }
            return arr.sort().join("");
        }

        let sign = getSignString(body); //获取签名字符串sign
        //验证密钥
        let confirmKey = "aec0613a3b8d4673cbd583be53d3af1d";
        //md5计算
        let md1 = md5(confirmKey + sign + confirmKey);
        return md1.toUpperCase();
    },
    getUrl(url, body) {
        var data = Object.assign({ appId: "5f63e04b2750d39bf10f3c71aafeee4c", time: Date.now() }, body);
        var url = host + url;
        var sign = this.getSign(data);
        return url + "?" + this.getQueryParam(sign, data);
    },
    query(url) {
        return fetch(url)
            .then(res => res.json())
            .then(res => {
                if (res.returnCode === 0) {
                    return res.result;
                }
                return {};
            });
    },
    queryAllTowner() {
        
        var body = {
            projectId: "395519115012096",
            deviceId: "7f983bd4-ad47-496c-ad0e-d2a430d02aee",
        };
        var url = `/gss/api/web/gss-v5-iot-rest-safe/projects/${body.projectId}/cranes/allInfo`;
        this.query(this.getUrl(url, body)).then(res => {
        });
    },
    queryTownerEffect() {
        var body = {
            eventDate:new Date().toLocaleDateString().replaceAll('/','-'),
            projectId: "395519115012096",
            deviceId: "7f983bd4-ad47-496c-ad0e-d2a430d02aee",
        };
        var url = `/gss/api/web/gss-v5-iot-rest-safe/projects/${body.projectId}/cranes/craneEffect/${body.eventDate}`;
        return this.query(this.getUrl(url, body))
    },
};
